# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def maxDepth(self, root: TreeNode) -> int:
        if root is None:
            return 0
        # 递归
        # return 1 + max(self.maxDepth(root.left), self.maxDepth(self.right))
        stack = [(root, 1)]
        d = 0
        while stack:
            node, depth = stack.pop()
            d = max(d, depth)
            if node.left:
                stack.append((node.left, depth+1))
            if node.right:
                stack.append((node.right, depth+1))
        return d
